package com.pgy.general.util;

import com.pgy.general.exceptions.LoginRuntimeException;
import com.pgy.general.exceptions.generalRuntimeException;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @Auther:superChao
 * @Date: 2021/03/19/12:35
 * @Version:2.0.0
 */
public class FileUtil {
    public static String upFile(MultipartFile file,String userid){
        if (file==null){
            throw new generalRuntimeException("上传文件不能为空");
        }
        String fileName = file.getOriginalFilename();
        String name =fileName.substring(0, fileName.indexOf("."));
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        String newFileName = name+"_"+System.currentTimeMillis()+"."+suffix;
        String basePath= PathUtil.getResultPath();
        File allpath=new File(basePath+userid);
        if(!allpath.exists()){
            allpath.mkdirs();
        }
        try{
            file.transferTo(new File(allpath+"/"+newFileName));
        }catch (IOException e){
            e.getMessage();
        }
        return "/file/"+userid+"/"+newFileName;
    }
}
